//
//  SMRequestManager.m
//  AFNetworking
//
//  Created by yons on 24.9.20.
//

#import "SMRequestManager.h"

@interface SMRequestManager ()

@end

static SMRequestManager *manager = nil;
static dispatch_once_t onceToken = 0;

@implementation SMRequestManager

/// 创建单例方法
+ (instancetype)shareManager {
    dispatch_once(&onceToken, ^{
        manager = [[SMRequestManager alloc] init];
    });
    return manager;
}

- (id)mutableCopyWithZone:(struct _NSZone *)zone {
    return [SMRequestManager shareManager];
}

#pragma mark -----------------------🍺🍺🍺----------------------- 封装数据请求 -----------------------🍺🍺🍺-----------------------

/// 类方法封装数据请求类(带进度条)
/// @param url url地址
/// @param method 请求方式
/// @param parameters 请求参数
/// @param downloadProgress 进度
/// @param success 成功回调
/// @param failure 失败回调
+ (NSURLSessionDataTask *)requestWithURL:(NSString *)url
                              httpMethod:(HttpMethod)method
                              parameters:(nullable id)parameters
                                progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
                                 success:(nullable void (^)(id _Nullable responseObject))success
                                 failure:(nullable void (^)(NSError *error))failure {
    return [[SMRequestManager shareManager] requestWithURL:url httpMethod:method parameters:parameters progress:downloadProgress success:success failure:failure];
}

/// 类方法封装数据请求类(不带进度条)
/// @param url url地址
/// @param method 请求方式
/// @param parameters 请求参数
/// @param success 成功回调
/// @param failure 失败回调
+ (NSURLSessionDataTask *)requestWithURL:(NSString *)url
                              httpMethod:(HttpMethod)method
                              parameters:(nullable id)parameters
                                 success:(nullable void (^)(id _Nullable responseObject))success
                                 failure:(nullable void (^)(NSError *error))failure {
    return [[SMRequestManager shareManager] requestWithURL:url httpMethod:method parameters:parameters progress:nil success:success failure:failure];
}

/// 类方法封装图片上传请求类
/// @param url url地址
/// @param parameters 参数
/// @param imageArray 图片数组
/// @param name 指定名称
/// @param success 成功回调
/// @param failure 失败回调
+ (NSURLSessionDataTask *)uploadImageWithURL:(NSString *)url
                                  parameters:(nullable id)parameters
                                  imageArray:(NSArray *)imageArray
                                        name:(NSString *)name
                                    progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
                                     success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                                     failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure {
    return [[AFHTTPSessionManager shareSessionManager] uploadImageWithURL:url parameters:parameters imageArray:imageArray name:name progress:downloadProgress success:success failure:failure];
}


/// 实例方法封装数据请求类
/// @param url url地址
/// @param method 请求方式
/// @param parameters 请求参数
/// @param downloadProgress 进度
/// @param success 成功回调
/// @param failure 失败回调
- (NSURLSessionDataTask *)requestWithURL:(NSString *)url
                              httpMethod:(HttpMethod)method
                              parameters:(nullable id)parameters
                                progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
                                 success:(nullable void (^)(id _Nullable responseObject))success
                                 failure:(nullable void (^)(NSError *error))failure {
    NSURLSessionDataTask *task;
    switch (method) {
        case HttpMethodGET:
        {
            [[AFHTTPSessionManager shareSessionManager] GET:url parameters:parameters headers:[[AFHTTPSessionManager shareSessionManager] headerParamsDic] progress:downloadProgress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                success(responseObject);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                failure(error);
            }];
        }
            break;
            
        case HttpMethodPOST:
        {
            [[AFHTTPSessionManager shareSessionManager] POST:url parameters:parameters headers:[[AFHTTPSessionManager shareSessionManager] headerParamsDic] progress:downloadProgress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                success(responseObject);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                failure(error);
            }];
        }
            break;
            
        case HttpMethodPUT:
        {
            [[AFHTTPSessionManager shareSessionManager] PUT:url parameters:parameters headers:[[AFHTTPSessionManager shareSessionManager] headerParamsDic] success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                success(responseObject);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                failure(error);
            }];
        }
            break;
            
        case HttpMethodDELETE:
        {
            [[AFHTTPSessionManager shareSessionManager] DELETE:url parameters:parameters headers:[[AFHTTPSessionManager shareSessionManager] headerParamsDic] success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                success(responseObject);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                failure(error);
            }];
        }
            break;
            
        default:
            break;
    }
    [task resume];
    return task;
}

@end
